<?php
##############################################
#
#   菜单配置
# @ Time：2016-12-9 - 上午12:11:44
# @ Author ：JiangYongWei <1085562810@qq.com>
# @ Encoding ：UTF-8 language ：PHP
#
##############################################

namespace Admin\Controller;
use Admin\Model\RuleModel;
use Admin\Model\UserGroupModel;


class RuleController extends AdminBaseController {
	
	protected $_ruleObject;
	
	public function _initialize(){
		parent::_initialize();
		$this -> _ruleObject  = new RuleModel();
	}
	
	/**
	 * 权限列表
	 */
	public function index(){
		$data = $this -> _ruleObject -> getTreeData('tree','id','title');
		$this -> assign('data',$data);
		$this -> display('index');
	}
	
	/**
	 * 添加权限
	 */
	public function addRule(){
		if(!IS_POST) return;
		$data=I('post.');
		$data['pid'] = isset($_POST['parent_id']) ? intval($_POST['parent_id']) : 0;
		$result = $this -> _ruleObject -> addRule($data);
		if($result)
			$this -> success('添加成功',U('index'),1);
		else
			$this -> error('操作失败'); 
	}
	
	/**
	 * 删除权限
	 */
	public function deleteRule(){
		if(!IS_AJAX) return;
		$result = $this -> _ruleObject -> deleteRule(intval($_GET['id']));
		if($result == 1)
			$this -> ajaxReturn(array('status' => 1,'message' => '该菜单有子权限，不可删除'));
		elseif($result == 2)
			$this -> ajaxReturn(array('status' => 2,'message' => '删除成功'));
		elseif($result == 3)
			$this -> ajaxReturn(array('status' => 3,'message' => '操作失败'));
	}
	
	/**
	 * 修改权限
	 */
	public function editRule(){
		$data = array(
			'id' => I('get.id'),
			'title' => I('post.title'),
			'name' => I('post.name')
		);
		$result = $this -> _ruleObject -> editRule($data);
		if($result)
			$this -> success('修改成功',U('index'),1);
		else
			$this -> error('操作失败');
	}
	
}